// pages/account/index.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        url: app.globalData.url,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options) {
            this.setData({ user: JSON.parse(options.user) })
        }
    },
    getGender(e) {
        if (e.detail.value) {
            let user = this.data.user
            if (user) {
                user.gender = e.detail.value
                this.setData({ user })
            }
        }
    },
    getName(e) {
        if (e.detail.value) {
            let user = this.data.user
            if (user) {
                user.username = e.detail.value
                this.setData({ user })
            }
        }
    },
    edit() {
        const that = this
        wx.showLoading({
            title: '请稍后',
        })
        let user = that.data.user
        user.gender = user.gender == 1
        console.log(that.data.user);
        wx.request({
            url: that.data.url + '/user/edit',
            method: 'POST',
            data: user,
            success(res) {
                if (res.data.code === 1) {
                    wx.hideLoading({
                        success: (res) => {
                            wx.showToast({
                                title: '修改成功',
                            })
                            wx.setStorageSync("user", that.data.user)
                            let pages = getCurrentPages();
                            let prevPage = pages[pages.length - 2];//上一个页面
                            wx.navigateBack({
                                delta: 0,
                            })
                        },
                    })
                }
            }
        })
    },

})